package com.yanqu.road.server.manger;

import com.yanqu.road.entity.config.goods.GoodsBagInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.logic.bussiness.servercenter.CommonBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigInteger;
import java.util.*;

public class TempCommonMgr extends TempMgr {

    private static Map<Integer, GoodsInfo> goodsInfoMap;

    private static Map<Integer, Map<Integer, List<GoodsBagInfo>>> goodsBagInfoMap;

    private static RandomHelper threadSafeRandom = new RandomHelper();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadData() {
        reloadCommonData();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    public static void reloadCommonData() {
        int channelId = ConfigHelper.getInt("channelId");
        Map<Integer, GoodsInfo> tempGoodsInfoMap = CommonBussiness.getGoodsInfoMap();
        //新加的通过渠道来区分不同的配置，防止测试服和正式服混了，合并一起根据id替换旧的
        Map<Integer, GoodsInfo> tempGoodsInfoMapChannel = CommonBussiness.getGoodsInfoMapChannel(channelId);
        goodsInfoMap = mergeGood(tempGoodsInfoMap,tempGoodsInfoMapChannel);

//        goodsInfoMap = CommonBussiness.getGoodsInfoMap();
        initGoodsInfoMap(goodsInfoMap);
        goodsBagInfoMap = mergeGoodBag(CommonBussiness.getGoodsBagInfoMap(),CommonBussiness.getGoodsBagInfoMapChannel(channelId));

        getLogger().info("TempCommonMgr reloadCommonData finish.");
    }

    private static  Map<Integer, GoodsInfo> mergeGood(Map<Integer, GoodsInfo> oldMap , Map<Integer, GoodsInfo> channelMap){
        for(GoodsInfo channelGoodsInfo : channelMap.values()){
            oldMap.put(channelGoodsInfo.getGoodsId(),channelGoodsInfo);
        }
        return oldMap;
    }

    private static  Map<Integer, Map<Integer, List<GoodsBagInfo>>> mergeGoodBag(Map<Integer, Map<Integer, List<GoodsBagInfo>>> oldMap , Map<Integer, Map<Integer, List<GoodsBagInfo>>> channelMap){
        for(Integer key : channelMap.keySet()){
            Map<Integer, List<GoodsBagInfo>> temp = channelMap.get(key);
            oldMap.put(key,temp);
        }
        return oldMap;
    }

    public static void initGoodsInfoMap(Map<Integer, GoodsInfo> tempGoodsInfoMap){
        for(GoodsInfo goodsInfo : tempGoodsInfoMap.values()){
            if((goodsInfo.getType() == eGoodsType.AddPatronsAbility.getValue() && 0 == goodsInfo.getParamList().get(0).intValue()) ||
                    (goodsInfo.getType() == eGoodsType.AddBeautyIntimacy.getValue() && 0 == goodsInfo.getParamList().get(0).intValue()) ||
                    (goodsInfo.getType() == eGoodsType.AddBeautyGlamour.getValue() && 0 == goodsInfo.getParamList().get(0).intValue()) ||
                    (goodsInfo.getType() == eGoodsType.AddBaiYeExp.getValue() && 0 == goodsInfo.getParamList().get(0).intValue()) ||
                    (goodsInfo.getType() == eGoodsType.AddTargetPatronsAbility.getValue() && 0 != goodsInfo.getParamList().get(0).intValue())||
                    (goodsInfo.getType() == eGoodsType.AddTargetPatronsQualification.getValue() && 0 != goodsInfo.getParamList().get(0).intValue())){
                List<NormalWeightItem> weightItemList = getNormalWeightItemList(goodsInfo.getExtendParam());
                goodsInfo.setWeightItemList(weightItemList);
            }
        }
    }

    /**
     * 获取礼包里面随机道具信息列表
     */
    public static List<GoodsBagInfo> getRandomGoodsList(int bagId) {
        List<GoodsBagInfo> resultGoodsList = new ArrayList<>();
        Map<Integer, List<GoodsBagInfo>> goodsMap = goodsBagInfoMap.get(bagId);
        if (null == goodsMap) {
            return null;
        }
        for (List<GoodsBagInfo> goodsList : goodsMap.values()) {
            int sumWeight = 0;
            for (GoodsBagInfo goodsBagInfo : goodsList) {
                sumWeight += goodsBagInfo.getRandomWeight();
            }
            if (sumWeight > 0) {
                int randomWeight = threadSafeRandom.next(0, sumWeight);
                int tempWeight = 0;
                for (GoodsBagInfo goodsBagInfo : goodsList) {
                    tempWeight += goodsBagInfo.getRandomWeight();
                    if (tempWeight > randomWeight) {
                        resultGoodsList.add(goodsBagInfo);
                        break;
                    }
                }
            }
        }
        return resultGoodsList;
    }

    public static List<List<GoodsBagInfo>> getMultipleRandomGoodsList(int bagId, int randomCount) {
        if (!goodsBagInfoMap.containsKey(bagId)) {
            return null;
        }
        List<List<GoodsBagInfo>> resultList = new ArrayList<>();
        Map<Integer, List<GoodsBagInfo>> goodsMap = new HashMap<>(goodsBagInfoMap.get(bagId));
        List<List<GoodsBagInfo>> randomList = new ArrayList<>();
        for (List<GoodsBagInfo> list : goodsMap.values()) {
            randomList.add(new ArrayList<>(list));
        }
        for (int i = 0; i < randomCount; i++) {
            List<GoodsBagInfo> randomResult = new ArrayList<>();
            for (List<GoodsBagInfo> bagInfoList : randomList) {
                int sumWeight = 0;
                for (GoodsBagInfo goodsBagInfo : bagInfoList) {
                    sumWeight += goodsBagInfo.getRandomWeight();
                }
                if (sumWeight > 0) {
                    int randomWeight = threadSafeRandom.next(0, sumWeight);
                    int tempWeight = 0;
                    Iterator<GoodsBagInfo> iterator = bagInfoList.iterator();
                    while (iterator.hasNext()) {
                        GoodsBagInfo bagInfo = iterator.next();
                        tempWeight += bagInfo.getRandomWeight();
                        if (tempWeight > randomWeight) {
                            randomResult.add(bagInfo);
                            iterator.remove();
                            break;
                        }
                    }
                }
            }
            if (randomResult.size() > 0) {
                resultList.add(randomResult);
            }
        }
        return resultList;
    }

    public static GoodsInfo getGoodsInfo(int goodsId) {
        return goodsInfoMap.get(goodsId);
    }

    public static List<GoodsInfo> getGoodsInfoList() {
        return new ArrayList<>(goodsInfoMap.values());
    }

    public static List<NormalWeightItem> getNormalWeightItemList(String NormalWeightItemParam) {
        List<NormalWeightItem> dataList = new ArrayList<>();
        if (null == NormalWeightItemParam) {
            return dataList;
        }
        String[] eventParamArr = NormalWeightItemParam.split("\\|");
        for (String eventParam : eventParamArr) {
            String[] param = eventParam.split(";");
            NormalWeightItem weight = new NormalWeightItem();
            weight.setValue(new BigInteger(param[0]));
            weight.setWeight(Integer.parseInt(param[1]));
            dataList.add(weight);
        }
        return dataList;
    }
}
